﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Customer
{
    public class ReceiveController : Controller
    {
        [ActionFilter(IsJson = true)]
        public JsonResult Index(String Ids, String KeyCodes)
        {
            if (!Fun.IDVerify(Ids, KeyCodes))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            int total = Ids.Split(',').Length*2;
            String userId = BasePage.GetUserId(Session);
            String date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            String fllowInterval = BasePage.getDict(Session).GetValue2("D02", "3");
            String[] sqls = new string[2];
            sqls[0] = String.Format("update cu_Customers set LatelyReceDate='{0}',LatelyReceId={1},custState=2 where custState=1 and id in ({2})", date, userId, Ids);
            sqls[1] = String.Format("update cu_Cus_Distribution set ReceiveDate='{0}',FllowInterval={1},State=1 where State=0 and cid in ({2})", date, fllowInterval, Ids);
            bool t = BasePage.bd.ExecuteSQL(sqls, total);
            return BasePage.Json(t ? ResultData.OK("领取成功") : ResultData.Error("领取失败"));           
        }
        [ActionFilter(IsJson =true)]
        public JsonResult Return(String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            int total = 2;
            String[] sqls = new string[2];
            sqls[0] = String.Format("update cu_Customers set custState=0 where custState=1 and id in ({0})", id);
            sqls[1] = String.Format("update cu_Cus_Distribution set State=2 where State=0 and cid in ({0})", id);
            bool t = BasePage.bd.ExecuteSQL(sqls, total);
            return BasePage.Json(t ? ResultData.OK("退回成功") : ResultData.Error("退回失败"));
        }
    }
}